<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文档管理</title>
    <link href="../css/mems-index.css" type="text/css" rel="stylesheet"/>
    <link rel="stylesheet" href="../css/doc-manage.css">
</head>
<body>

<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a href="doc-manage.html">文档管理</a>&nbsp;>&nbsp;文档管理</div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
        <!--<li id="promanage">-->
            <!--<div class="menu-first"><i class="project"></i>项目管理</div>-->
            <!--<dl class="menu-second">-->
                <!--<dd><a href="mems-pro-list.html" id="prolit">项目列表</a></dd>-->
            <!--</dl>-->
        <!--</li>-->
        <!--<li id="needmange">-->
            <!--<div class="menu-first"><i class="need"></i>需求管理</div>-->
            <!--<dl class="menu-second">-->
                <!--<dd><a href="pm-need-list.html" id="needlist">需求池</a></dd>-->
                <!--<dd><a href="pm-bug-list.html" id="buglist">缺陷池</a></dd>-->
            <!--</dl>-->
        <!--</li>-->
        <!--<li id="system">-->
            <!--<div class="menu-first"><i class="system"></i>系统配置</div>-->
            <!--<dl class="menu-second">-->
                <!--<dd><a href="user-manage.html" id="usermanage">用户管理</a></dd>-->
                <!--<dd><a href="organize-architecture.html" id="organize">组织架构</a></dd>-->
                <!--<dd><a href="tag-manage.html" id="tagmanage">标签管理</a></dd>-->
            <!--</dl>-->
        <!--</li>-->
        <!--<li id="documentmanage">-->
            <!--<div class="menu-first"><i class="project"></i>文档管理</div>-->
            <!--<dl class="menu-second">-->
                <!--<dd><a href="doc-manage.html" id="doc-manage">文档管理</a></dd>-->
            <!--</dl>-->
        <!--</li>-->
    </ul>
</aside>


<!--content-->
<div class="content">
    <div class="page-inner">
        <div class="item-content">
            <div class="table-box">
                <table>
                    <thead>
                        <tr>
                            <td colspan="2">
                                <a href="javascript:;" class="a-upload" id="fileUpload"><input id="file" value="请选择" type="file"/>上传文件</a>
                            </td>
                            <td>
                                <div class="total-right">
                                    <div class="search-box">
                                        <form action="javascript:;" id="search-user-form">
                                            <input type="text" placeholder="搜索文件" id="tagName-text">
                                            <div class="search-icon" id="search-icon" onclick="getDocList(1);"></div>
                                        </form>
                                    </div>
                                    <span class="display-middle">
                                        共有 <span class="total-num"></span> 个文件
                                    </span>
                                </div>
                            </td>
                        </tr>
                    </thead>
                    <tbody id="bodyList">

                    </tbody>
                </table>
            </div>
            <div class="pages clear">
                <div class="page-list clear" id="pageList">

                </div>
            </div>
        </div>
    </div>
</div>
<div class="user-mask"></div>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/common.js"></script>
<script src="../js/user-tree.js"></script>
<script type="text/javascript">
    var url  = _ip+'/file/uploading';
    var attachment = new attachment();
    attachment.init({
        url: url,
        fileUpload:$('#fileUpload'),
        addAttachmentList:$('#addAttachmentList'),
        noFormat: true,
        success: function(json) {
            if (json.success) {
                getDocList(1);
            }
        }
    });

    $('#search-user-form').on('submit', function() {
        getDocList(1);
       return false;
    });
    var roleid = $.cookie('userrole')
    function getDocList(p, checPara) {
        var PerPage = 20;
        var name = $('#tagName-text').val();
        var url  = _ip+'/file/fileList?page='+p+'&size='+PerPage + '&name='+ name;
        var data = checPara || {};
        $('#bodyList').html('');
        organizeAjaxPost(url, data, function(json) {
            $('.total-num').text(json.total || 0);
            if(!json.data || json.data.length <= 0){
                $('#bodyList').html('<tr><td colspan="3" style="text-align: center">暂无数据！</td></tr>');
                $('#pageList').html('');
                return false;
            }
            var _data     = [];
            var _pager    = [];
            var userName= $('#userName-text').val();
            var totalPage = json.totlePage;
            var prev      = p-1;
            if(prev < 1)prev = 1;
            var next = p + 1;
            if(next > totalPage)next = totalPage;
            for(var k = 0, kk = json.data.length; k < kk; k++){
                var msg = json.data[k];
                var size_text = '';
                if (msg.size >= 1024*1024*1024) {
                    size_text = parseFloat((msg.size / (1024*1024*1024)).toFixed(2)) + 'G';
                }
                else if(msg.size >= 1024*1024) {
                    size_text = parseFloat((msg.size / (1024*1024)).toFixed(2)) + 'M';
                }
                else if(msg.size >= 1024) {
                    size_text = parseFloat((msg.size / 1024).toFixed(2)) + 'KB';
                }
                else {
                    size_text = parseFloat(msg.size.toFixed(2)) + 'B';
                }
                var msgData = {
                    id: msg.id,
                    name: msg.name,
                    size: msg.size,
                    size_text: size_text,
                    path: msg.path
                };
                _data[k] = $('<tr>' +
                        '<td class="organize">' +
                        '<span>'+ msg.name +'</span>' +
                        '<span class="right">' +
                        '<a href="'+_ip +'/file/download?path='+ msg.path +'">' +
                        '<i class="download display-middle" onclick=""></i>' +
                        '</a>'+(roleid==9 || roleid==3?'<i class="delete-icon display-middle" onclick="deleteDoc(this); event.stopPropagation();"></i>':'') +
                        '</span>' +
                        '</td>' +
                        '<td>'+ msgData.size_text +'</td>' +
                        '<td>上传于 '+ msg.createTime+'</td>' +
                        '</tr>');
/*                _data.push("<tr data-edit="+ JSON.stringify(msgData) +">");
                _data.push('<td class="organize">' +
                        '<span>'+ msg.name +'</span>' +
                        '<span class="right">' +
                        '<a href="'+_ip +'/file/download?path='+ msg.path +'">' +
                        '<i class="download display-middle" onclick=""></i>' +
                        '</a><i class="delete-icon display-middle" onclick="deleteDoc(this); event.stopPropagation();"></i>' +
                        '</span>' +
                        '</td>');
                _data.push('<td>'+ msgData.size_text +'</td>');
                _data.push('<td>上传于 '+ msg.createTime+'</td>');*/
                var $tr = _data[k].data('edit', msgData);
                $('#bodyList').append($tr).data('page', p);
            }
            _pager.push('<span>共：'+ totalPage +'页</span>');
            _pager.push('<span>您正在第：'+p+'页</span>');
            _pager.push('<a href="javascript:;" class="page-pre" onclick="pageClick('+prev+ ');return !1;">上一页</a>');
            _pager.push('<a href="javascript:;" class="page-next" onclick="pageClick('+next+');return !1;">下一页</a>');
            _pager.push('<span>前往第</span>');
            _pager.push('<input type="text" id="goToPage"/>');
            _pager.push('<span>页</span>');
            _pager.push('<button id="goToPageBtn">GO</button>');
            $('#pageList').html(_pager.join(""));
            $('#goToPageBtn').on('click',function () {
                var v = $('#goToPage').val();
                if(!/^[1-9]\d*$/.test(v)){
                    return false;
                }
                if(v>1 && v<totalPage){
                    getDocList(v,{name: userName});
                }
                if(v <= 1){
                    getDocList(1,{name: userName});
                    return false;
                }
                if(v >= totalPage){
                    getDocList(totalPage,{name: userName});
                    return false;
                }
            })

        });
    }

    getDocList(1);


    function pageClick(page) {
        getDocList(page, {});
    }

    function downDocClick(obj) {
        var $obj = $(obj);
        var path = $obj.closest('tr').data('edit').path;
        var url  = _ip+'/file/download?path='+ path;
        organizeAjaxGet(url, {  }, '', function(json) {

        })
    }

    function deleteDoc(obj) {
        if(globleRoleId == 0 || globleRoleId == 2){
            alert('您没有权限删除文件！');
            return;
        }
        var $obj = $(obj);
        var path = $obj.closest('tr').data('edit').path;
        var id = $obj.closest('tr').data('edit').id;
        var url  = _ip+'/file/deleteFile?path='+ path + '&id='+ id;
        organizeAjaxGet(url, {  }, '', function(json) {
            if(json.success) {
                successAlert('删除文件成功！');
                getDocList(1, {});
            }
        })
    }

//    普通用户不能上传文件
    function unUploadFile() {
        if(roleid == 2 || roleid == 1){
            $('#fileUpload').remove()
        }
    }
    unUploadFile()
</script>
</body>
</html>